{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### nan和inf(day10练习过)",
   "id": "fe570904ae4d4987"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:22:23.787982Z",
     "start_time": "2025-01-07T10:22:23.777796Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#nan缺失数据\n",
    "#inf无穷大\n",
    "import numpy as np\n",
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a, type(a))\n",
    "print(b, type(b))\n"
   ],
   "id": "e1512d856084eb6b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan <class 'float'>\n",
      "inf <class 'float'>\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:20:48.096956Z",
     "start_time": "2025-01-07T10:20:48.077038Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#nan不等于nan\n",
    "print(np.nan == np.nan)\n",
    "print(True == 1)\n",
    "print(False == 0)\n",
    "#nan和其他数运算的结果都是nan\n",
    "np.nan + 1  "
   ],
   "id": "eb555158cef30455",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:28:38.101950Z",
     "start_time": "2025-01-07T10:28:38.081031Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#判断nan个数\n",
    "t = np.arange(24, dtype=float).reshape(4, 6)\n",
    "t[3, 4] = np.nan\n",
    "t[2, 4] = np.nan\n",
    "\n",
    "# print(t!= t)        \n",
    "#这个表达式 t！= t.\n",
    "# 意思是检查数组中每个元素是否等于它自己。\n",
    "# 但是检查到nan时，nan不等于nan是true的。故只有nan那里结果为true\n",
    "\n",
    "# m=np.mean(t) #求nan的平均值，其中包括了nan\n",
    "\n",
    "\n",
    "print(\"原始数组:\")\n",
    "print(t)\n",
    "print('-'*50)\n",
    "\n",
    "# 计算非 NaN 元素的平均值,一定要是非nan元素！否则会依旧输出nan！！！\n",
    "m = np.nanmean(t)\n",
    "\n",
    "t[np.isnan(t)] = m\n",
    "\n",
    "# 打印替换后的数组\n",
    "print(\"替换 NaN 后的数组:\")\n",
    "print(t)"
   ],
   "id": "6d7ffeeca707171b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组:\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. nan 17.]\n",
      " [18. 19. 20. 21. nan 23.]]\n",
      "--------------------------------------------------\n",
      "替换 NaN 后的数组:\n",
      "[[ 0.          1.          2.          3.          4.          5.        ]\n",
      " [ 6.          7.          8.          9.         10.         11.        ]\n",
      " [12.         13.         14.         15.         10.81818182 17.        ]\n",
      " [18.         19.         20.         21.         10.81818182 23.        ]]\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:21:12.026674Z",
     "start_time": "2025-01-07T10:21:12.016373Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(np.inf == np.inf)\n",
    "np.inf"
   ],
   "id": "c13c0edf67d500b7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "inf"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T10:21:22.915469Z",
     "start_time": "2025-01-07T10:21:22.900408Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([-1, 0])\n",
    "print(arr)\n",
    "print(arr[0] / arr[1])  #1除0就会得到inf"
   ],
   "id": "624dc4a0c6ac1910",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1  0]\n",
      "-inf\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\YWH\\AppData\\Local\\Temp\\ipykernel_5224\\773640058.py:3: RuntimeWarning: divide by zero encountered in scalar divide\n",
      "  print(arr[0] / arr[1])  #1除0就会得到inf\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 转置和轴交换/轴滚动",
   "id": "5c50f10d50292d70"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:29:43.460848Z",
     "start_time": "2025-01-07T09:29:43.451823Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#对换数组的维度\n",
    "import numpy as np\n",
    "\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "print('原数组：')\n",
    "print(a)\n",
    "# print('\\n')\n",
    "\n",
    "print('对换数组：')\n",
    "# print(np.transpose(a))#相当于转置了，和a.T一样\n",
    "print(a)\n",
    "\n",
    "# 与transpose一致\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "\n",
    "# print('原数组：')\n",
    "# print(a)\n",
    "# print('\\n')\n",
    "\n",
    "print('转置数组：')\n",
    "print(a.T)"
   ],
   "id": "3e6e030ab1ab2b8b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "对换数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "转置数组：\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:30:24.663967Z",
     "start_time": "2025-01-07T09:30:24.656163Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 函数用于交换数组的两个轴\n",
    "t1 = np.arange(24).reshape(4, 6)\n",
    "re1 = t1.swapaxes(1, 0)#交换第0轴和第1轴(行列互换)\n",
    "\n",
    "print(' 原 数 组 ：')\n",
    "print(t1)\n",
    "print('\\n')\n",
    "print(re1.shape)\n",
    "print('调用 swapaxes 函数后的数组：')\n",
    "print(re1)\n"
   ],
   "id": "30f6b04de85954cb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 原 数 组 ：\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "\n",
      "(6, 4)\n",
      "调用 swapaxes 函数后的数组：\n",
      "[[ 0  6 12 18]\n",
      " [ 1  7 13 19]\n",
      " [ 2  8 14 20]\n",
      " [ 3  9 15 21]\n",
      " [ 4 10 16 22]\n",
      " [ 5 11 17 23]]\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:32:32.318348Z",
     "start_time": "2025-01-07T09:32:32.307710Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#如果是3维及以上，称为轴交换\n",
    "t3 = np.arange(60).reshape(3, 4, 5)\n",
    "print(t3.shape)\n",
    "print('-' * 50)\n",
    "t3 = np.swapaxes(t3, 1, 2)#012，记住是从0开始\n",
    "print(t3.shape)\n",
    "# print(t3) 数据不用记住，不用观察"
   ],
   "id": "4a5899cb4ac216d4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 4, 5)\n",
      "--------------------------------------------------\n",
      "(3, 5, 4)\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:33:34.015921Z",
     "start_time": "2025-01-07T09:33:34.009836Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数组的轴滚动,swapaxes每次只能交换两个轴，没有rollaxis方便，默认情况下轴滚动最前面\n",
    "a = np.ones((3, 4, 5, 6))\n",
    "# np.rollaxis(a, 2).shape\n",
    "b = np.rollaxis(a, 3, 1) # 将3号轴“滚动”到1号位\n",
    "print(b.shape)"
   ],
   "id": "3f9302275c2a0a6b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 6, 4, 5)\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:35:45.432328Z",
     "start_time": "2025-01-07T09:35:45.416075Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#数据拷贝，copy()和赋值的区别\n",
    "b = np.array([[1, 2, 3], [1, 2, 3]])\n",
    "a = b.copy()\n",
    "a"
   ],
   "id": "c42d5c016969b681",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [1, 2, 3]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:35:55.491031Z",
     "start_time": "2025-01-07T09:35:55.481596Z"
    }
   },
   "cell_type": "code",
   "source": [
    "b[0, 0] = 3#copy()，原数组改变不会影响copy数组\n",
    "print(b)\n",
    "a"
   ],
   "id": "2b31e5425fecbd3c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 2 3]\n",
      " [1 2 3]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [1, 2, 3]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 文件",
   "id": "903e088c8af28220"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T09:38:23.000997Z",
     "start_time": "2025-01-07T09:38:22.994529Z"
    }
   },
   "cell_type": "code",
   "source": [
    "names = 'zhangsan,lisi,wangwu,zhaoliu,sunqi'\n",
    "#写入文件\n",
    "with open('names.csv', 'w') as f: #会新建文件，如果文件存在，则覆盖\n",
    "    f.write(names)\n",
    "    f.write('\\n')\n",
    "    f.write('12,23,34,45,56')\n",
    "\n"
   ],
   "id": "98e5ca9ca4be38fe",
   "outputs": [],
   "execution_count": 11
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
